//
//  RHAgencyApplyInfoCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/7/25.
//

#import "RHAgencyApplyInfoCell.h"
#import "RHFileCell.h"
#import <MGSwipeButton.h>

@interface RHAgencyApplyInfoCell()<QMUITextViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UILabel *desTitleLab;

@property (nonatomic,strong) QMUITextView *textView;

@property (nonatomic,strong) UILabel *attachTitleLab;



@property (nonatomic,strong) QMUIButton *addBtn;

@property (nonatomic,strong) UIView *bgView;

@property (nonatomic,strong) QMUILabel *countLab;//文字个数

@property (nonatomic ,strong) UITableView *tableView;
@end
@implementation RHAgencyApplyInfoCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}
-(void)setData:(RHAgencyApplyModel *)data
{
    _data = data;
    if(data.des_content.length>0){
        self.textView.text = data.des_content;
    }else{
        self.textView.text = nil;
        self.textView.placeholder = data.des_placeHold;
    }
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(self.data.attachments.count==0?40:self.data.attachments.count*40);
    }];
    [self.tableView reloadData];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length>0){
        self.data.des_content = textView.text;
    }
}

-(void)hideAddBtn
{
    _textView.userInteractionEnabled = NO;
    self.addBtn.hidden = YES;
    [self.addBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(0);
        
    }];
    
}

-(void)setupUI
{
    self.contentView.backgroundColor = CFTabViewBgColor;
    _bgView = [[UIView alloc]init];
    _bgView.layer.cornerRadius = 8;
    _bgView.layer.masksToBounds = YES;
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    kWeakSelf;
    self.desTitleLab = kLab;
    self.desTitleLab.text = @"Describe";
    self.desTitleLab.font = [UIFont systemFontOfSize:15];
    self.desTitleLab.textAlignment = NSTextAlignmentLeft;
    [self.bgView addSubview:self.desTitleLab];
    [self.desTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.bgView).offset(10);
        make.top.equalTo(weakSelf.bgView).offset(10);
    }];
    
    self.textView = [[QMUITextView alloc]init];
    self.textView.layer.cornerRadius = 8;
    self.textView.layer.masksToBounds = YES;
    self.textView.backgroundColor = kRGB(244, 244, 244);
    self.textView.textColor = kRGB(90, 90, 90);
    self.textView.placeholderColor = [UIColor colorWithHexString:@"3F4443" alpha:0.5];
    self.textView.maximumTextLength = 200;
    [self.bgView addSubview:self.textView];
    self.textView.delegate = self;
    [[RACSignal merge:@[self.textView.rac_textSignal,RACObserve(self.textView, text)]]subscribeNext:^(NSString  *_Nullable text) {
          
        weakSelf.countLab.text = [NSString stringWithFormat:@"%ld/200",text.length];
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.desTitleLab);
        make.top.equalTo(weakSelf.desTitleLab.mas_bottom).offset(10);
        make.right.equalTo(weakSelf.bgView).offset(-10);
        make.height.offset(90);
    }];
    
    self.countLab = [[QMUILabel alloc]init];
    [self.countLab setTextColor:[UIColor colorWithHexString:@"3F4443" alpha:0.4]];
    [self.bgView addSubview:self.countLab];
    self.countLab.font = [UIFont systemFontOfSize:11];
    self.countLab.text = @"0/200";
    [self.countLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.textView).offset(-3);
        make.right.equalTo(weakSelf.textView).offset(-3);
        
    }];
    
    self.attachTitleLab = kLab;
    self.attachTitleLab.text = @"attachment:";
    self.attachTitleLab.font = [UIFont systemFontOfSize:15];
    self.attachTitleLab.textAlignment = NSTextAlignmentLeft;
    [self.bgView addSubview:self.attachTitleLab];
    [self.attachTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.desTitleLab);
        make.top.equalTo(weakSelf.textView.mas_bottom).offset(15);
    }];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [_tableView registerClass:[RHFileCell class] forCellReuseIdentifier:@"RHFileCell"];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.bgView addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.desTitleLab);
        make.top.equalTo(weakSelf.attachTitleLab.mas_bottom).offset(10);
        make.right.equalTo(weakSelf.bgView.mas_right).offset(-10);
        make.height.mas_equalTo(40);
    }];

    
    QMUIButton *addBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];;
    [addBtn setImage:[UIImage imageNamed:@"add_file"] forState:UIControlStateNormal];
        [addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [weakSelf.bgView addSubview:addBtn];
    self.addBtn = addBtn;
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(35);
        make.height.equalTo(addBtn.mas_width);
        make.top.mas_equalTo(weakSelf.tableView.mas_bottom).offset(15);
        make.centerX.equalTo(weakSelf.bgView);
    }];

    [[addBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.openFileBlock){
            weakSelf.openFileBlock();
        }
    }];
    
    //封底
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.bgView).offset(-5);
    }];
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.attachments.count==0?1:self.data.attachments.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    RHFileCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHFileCell"];
    RHPdfFileModel *model;
    if(self.data.attachments.count == 0){
        model = [[RHPdfFileModel alloc]init];
    }else{
       model =  self.data.attachments[indexPath.row];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.fileModel = model;
    kWeakSelf;
    MGSwipeButton *deleteBtn = [MGSwipeButton buttonWithTitle:@"Delete" icon:[UIImage imageNamed:@""] backgroundColor:FontColor(@"#F790A1") padding:18 callback:^BOOL(MGSwipeTableCell * _Nonnull cell) {
        if(weakSelf.data.attachments.count==0)return YES;
        [weakSelf.data.attachments removeObjectAtIndex:indexPath.row];
        [weakSelf.tableView reloadData];
        if(weakSelf.refreshBlock){
            weakSelf.refreshBlock();
        }
        return YES;
    }];
    cell.rightButtons = @[deleteBtn];
    
    return cell;
}






- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    
}

@end
